package com.btpj.module_main.ui.tree

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.btpj.jetpack.base.BaseViewModel
import com.btpj.jetpack.utils.ToastUtil
import com.btpj.jetpack.entity.ApiResult
import com.btpj.module_main.data.entity.TreeData
import com.btpj.module_main.data.remote.repository.MainRepository
import kotlinx.coroutines.launch

class TreeViewModel(private val repository: MainRepository) : BaseViewModel() {

    val treeDataLiveData = MutableLiveData<MutableList<TreeData>>()

    private fun getTreeList() {
        viewModelScope.launch {
            when (val treeData = repository.getTreeList()) {
                is ApiResult.Success -> treeDataLiveData.postValue(treeData.data)
                is ApiResult.Error -> ToastUtil.showShort(treeData.exception.errorMsg ?: "请求失败")
            }
        }
    }

    override fun start() {
        getTreeList()
    }
}